/*
 * Copyright (c) 2016. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.techindata.cacheredis;

import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * <p>
 * Created by yanshen@tsinghuabigdata.com on 2016/2/23.
 * </p>
 *
 * @author yanshen@tsinghuabigdata.com
 * @version V1.0
 * @packageName: com.tsinghuabigdata.cacheredis
 * @createTime: 2016/2/23 16:21
 */
public class CacheSet {
    static Logger logger = Logger.getLogger(CacheSet.class.getName());

    /**
     * 返回所有成员
     *
     * @param key
     * @return
     */
    public static Set<String> smembers(String key) {
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.smembers(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        } finally {
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }

    public static Long put(String key, String... value){
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.sadd(key, value);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        } finally {
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }

    public static Long srem(String key, String... value){
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.srem(key, value);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        } finally {
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }

}
